/**
 * egret引擎入口
 */
class Main extends eui.UILayer {

    protected createChildren(): void {
        super.createChildren();

        egret.lifecycle.addLifecycleListener((context) => {
            // custom lifecycle plugin
        })

        egret.lifecycle.onPause = () => {
            //egret.ticker.pause();
        }

        egret.lifecycle.onResume = () => {
            //egret.ticker.resume();
        }

        //inject the custom material parser
        //注入自定义的素材解析器
        let assetAdapter = new AssetAdapter();
        egret.registerImplementation("eui.IAssetAdapter", assetAdapter);
        egret.registerImplementation("eui.IThemeAdapter", new ThemeAdapter());

        this.runGame().catch(e => {
            console.log(e);
        })
    }

    private async runGame() {
        await this.loadResource();
        this.createGameScene();
        const result = await RES.getResAsync("description_json");
    }

    private async loadResource() {
        try {
            await RES.loadConfig("resource/default.res.json", "resource/");
            await RES.loadGroup("loadingRes", 0);
            await this.loadTheme();

            //绑定场景管理
            SceneManager.getInstance().rootLayer = this;
            //进入载入界面
            const loadingView = new loadingScene();
            SceneManager.getInstance().changeScene(loadingView);
            await RES.loadGroup("gameRes", 0, loadingView);
        }
        catch (e) {
            console.error(e);
        }
    }

    private loadTheme() {
        return new Promise((resolve, reject) => {
            // load skin theme configuration file, you can manually modify the file. And replace the default skin.
            //加载皮肤主题配置文件,可以手动修改这个文件。替换默认皮肤。
            let theme = new eui.Theme("resource/default.thm.json", this.stage);
            theme.addEventListener(eui.UIEvent.COMPLETE, () => {
                resolve();
            }, this);
        })
    }

    /**
     * 创建场景界面
     * Create scene interface
     */
    protected createGameScene(): void {
        //只允许chrome和safari浏览器进入
        if (!this.checkBrowser()) {
            alert("请使用Chrome或Safari内核浏览器打开！");
            window.open('', '_self');
            window.close();
            return;
        }
        else {
            //nano初始化
            this.nanoInit();
            //进入登录界面
            SceneManager.getInstance().changeScene(loginScene.getInstance());
        }
    }
    //websocket初始化
    /*'81.68.207.211'*/
    private nanoInit() {
        nano.init({
            host: "81.68.207.211",
            port: 9002,
            path: "/mid"
        }, () => {
        });
        //被顶掉的消息
        nano.on("tickout", async (data) => {
            globalObject.getInstance().debugConsole("被踢下线", data);
            SceneManager.getInstance().closeWindow();
            await SceneManager.getInstance().changeScene(loginScene.getInstance());
            globalObject.getInstance().showTip(data);
            return;
        });
        //博饼活动结束 弹窗提示
        nano.on("midend", async (data) => {
            globalObject.getInstance().debugConsole("midend", data);
            SceneManager.getInstance().closeWindow();
            await SceneManager.getInstance().changeScene(new introScene());
            SceneManager.getInstance().showWindow(new tipWindow({ text: data, btnText: "确定" }));
        })
    }
    //区分浏览器
    checkBrowser() {
        //取得浏览器的userAgent字符串
        let userAgent = navigator.userAgent;
        globalObject.getInstance().debugConsole("浏览器UA ", userAgent);
        if (userAgent.indexOf("Chrome") > -1) {
            return true;
        }
        else if (userAgent.indexOf("Edg") > -1) {
            return true;
        }
        else if (userAgent.indexOf("Mozilla") > -1) {
            return true;
        }
        else {
            return false;
        }
    }
}


